import base from "@src/store/base";
import {getReceivePageInfo, receiveBizCoupon} from "@src/api/coupon";
import pageHandleUtil from "@src/common/pageHandleUtil";
import color from "@src/common/color";
import router from "@src/router";

const {storeBindingsBehavior} = require("mobx-miniprogram-bindings");
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'businessName'],
  }],
  data: {
    isSendValid: true,
    navBarHeight: 80,
    noValidTitle: "",
  },
  methods: {
    onLoad(options) {
      let identityCode = '', shopId = 0;
      if (options.identityCode) {
        identityCode = decodeURIComponent(options.identityCode);
      }
      if (options.shopId) {
        shopId = decodeURIComponent(options.shopId);
      }
      this.setData({shopId, identityCode});
    },
    onLogin() {
      this.fetchData();
    },
    fetchData() {
      let data = {
        identityCode: this.data.identityCode,
        shop: this.data.shopId
      };
      getReceivePageInfo(data).then(res => {
        if (res.code === 0) {
          this.parseCoupon(res.data.couponDetail)
        } else {
          this.setData({isSendValid: false, noValidTitle: res.msg})
        }
      })
    },
    parseCoupon(couponDetail) {
      let availablePeriods = couponDetail.availablePeriods;
      let couponPrototype = couponDetail.couponPrototype;
      let showUseTimeArr = [];

      //时段可使用渲染
      if (couponPrototype.hasTimeLimit) {
        if (availablePeriods[0].timeType === "week") {
          showUseTimeArr = pageHandleUtil.dillCouponWeek(availablePeriods);
        } else {
          showUseTimeArr = pageHandleUtil.dillCouponDay(availablePeriods);
        }
      } else {
        showUseTimeArr.push({returnString: `全部时段可用`, timeString: ``});
      }
      //开始生效
      /*领取券是否立即生效fixedBeginTerm*/
      if (couponPrototype.fixedBeginTerm === 0) {
        showUseTimeArr.push({returnString: `券领取后立即生效`, timeString: ``});
      } else if (couponPrototype.fixedBeginTerm === 1) {
        showUseTimeArr.push({returnString: `券领取后第二天开始生效`, timeString: ``});
      } else {
        showUseTimeArr.push({returnString: `券领取后第${couponPrototype.fixedBeginTerm + 1}天开始生效`, timeString: ``});
      }

      /*生效后的时间范围或固定天数*/
      if (couponPrototype.dateType === "DATE_TYPE_FIX _TIME_RANGE") {
        //时间范围
        showUseTimeArr.push({
          returnString: `券的有效时间`, timeString: `${couponPrototype.beginTime}-${couponPrototype.endTime}`
        });
      } else if (couponPrototype.dateType === "DATE_TYPE_FIX_TERM") {
        //固定时间
        showUseTimeArr.push({returnString: `券领取后${couponPrototype.fixedTerm}天内有效`, timeString: ``});
      }
      //券的描述
      showUseTimeArr.push({returnString: couponPrototype.description, timeString: ``});

      let receiveStatus = couponPrototype.status;
      let initReceiveBtn = "立即领取";
      if (receiveStatus === 1) {
        initReceiveBtn = "立即领取";
      } else if (this.receiveStatus === 2) {
        initReceiveBtn = "暂不可领取";
      } else if (this.receiveStatus === 3) {
        initReceiveBtn = "已过期";
      }
      //处理背景色
      this.bgRender(couponPrototype.color);
      this.setData({receiveStatus, initReceiveBtn, showUseTimeArr, couponPrototype})
    },
    handleReceive() {
      wx.showLoading({title: "领取中...", mask: true});
      let data = {
        shopId: this.data.shopId,
        identityCode: this.data.identityCode
      };
      if (!this.data.shopId) {
        delete data.shopId;
      }
      receiveBizCoupon(data).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          let couponCode = res.data;
          wx.showModal({
            title: "提示",
            content: "领取成功",
            cancelText: "继续领取",
            confirmText: "去使用",
            success: (res) => {
              if (res.confirm) {
                router.push({
                  name: "member/couponDetail", params: {couponCode: couponCode, couponStatus: 1}
                })
              } else if (res.cancel) {
                this.fetchData();
              }
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 3000
          });
        }
      })
    },
    //背景渲染
    bgRender(gcolor) {
      //修改背景颜色
      let colorObj = color.getThemeObj(gcolor);
      wx.setBackgroundColor({
        backgroundColorTop: colorObj.color, // 顶部窗口的背景色为白色
        backgroundColorBottom: colorObj.color // 底部窗口的背景色为白色
      });
    }
  }
})
